package com.ketangpai.admin.controller;

import com.ketangpai.admin.entity.Course;
import com.ketangpai.admin.entity.param.CreateCourseParam;
import com.ketangpai.admin.entity.param.DropCourseParam;
import com.ketangpai.admin.entity.param.UpdateCourseParam;
import com.ketangpai.admin.service.CourseService;
import com.ketangpai.admin.utils.JsonResult;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/course")
@Api(tags = "课程管理")
public class CourseController {
    private CourseService courseService;

    public CourseController(CourseService courseService) {
        this.courseService = courseService;
    }

    @PostMapping("/getById")
    public JsonResult getCourseById(@RequestParam Integer courseId){

        Course course=courseService.getCourseById(courseId);
        return JsonResult.success(course);
    }
    @PostMapping("/update")
    public  JsonResult update(@RequestBody UpdateCourseParam updateCourseParam){
        boolean result=courseService.update(updateCourseParam);
        if(result){
            return JsonResult.success("编辑成功");
        }else return JsonResult.error("编辑失败");
    }
    @PostMapping("/create")
    public JsonResult create(@RequestBody Course course){
        Course createCourse=courseService.create(course);
        return JsonResult.success("创建成功");
    }

    //删除课程
    @PostMapping("/delete")
    public JsonResult delete(@RequestBody DropCourseParam dropCourseParam){
        String result=courseService.delete(dropCourseParam);
        if(result.equals("删除成功")){
            return JsonResult.success("删除成功");
        }else if (result.equals("密码错误")){
            return JsonResult.error("密码错误");
        }else {
            return JsonResult.error("删除失败");
        }
    }
}
